package com.ruoyi.gd.khbjk.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gd.khbjk.domain.bo.ZKhbjkBo;
import com.ruoyi.gd.khbjk.domain.vo.ImportZKhbjkVo;
import com.ruoyi.gd.khbjk.domain.vo.ZKhbjkVo;

import java.util.ArrayList;
import java.util.List;

public class KhbjkRowsListener extends AnalysisEventListener<ImportZKhbjkVo>{

    private List<ImportZKhbjkVo> zkhbjkList = new ArrayList<>();

    private int skipRows; // 要跳过的行数
    private ZKhbjkBo zkhbjkBo; // 要跳过的行数
    private boolean isSkipped = false;

    public KhbjkRowsListener(int skipRows, ZKhbjkBo zkhbjkBo) {
        this.skipRows = skipRows;
        this.zkhbjkBo = zkhbjkBo;
    }

    @Override
    public void invoke(ImportZKhbjkVo data, AnalysisContext context) {
        try {
            // 当前行
            int currentRowNum = context.readRowHolder().getRowIndex();
            if (currentRowNum >= skipRows && StringUtils.isNotEmpty(data.getXh()) && StringUtils.isNotEmpty(data.getName()) && StringUtils.isNotEmpty(data.getXhgg())) {
                data.setKhid(zkhbjkBo.getKhid());
                data.setKhname(zkhbjkBo.getKhname());
                data.setKhdz(zkhbjkBo.getKhdz());
                data.setKhdh(zkhbjkBo.getKhdh());
                // 去除当前实体类中的所有数据的空格
                StringUtils.trimEntityFields(data);
                zkhbjkList.add(data);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println(context);
        System.out.println("数据遍历完毕");
        // 所有数据解析完成后的操作
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        System.out.println(extra);
        System.out.println(context);
    }

    public List<ImportZKhbjkVo> getZkhbjkList() {
        return zkhbjkList;
    }

}
